<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />



  <meta name="google-site-verification" content="RNVwJJw32eotPkPUqLM4rell2tXbic1eMfSTDaYi7sw" />



  <meta name="msvalidate.01" content="914C9C1CBC7078C5E2AC612AD5C1CC6C" />












  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.3" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.3">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.3" color="#222">





  <meta name="keywords" content="思考,区块链,个人成长" />










<meta name="description" content="关注领域：个人成长、思考、总结、区块链，Linux，移动开发，Mac">
<meta name="keywords" content="思考,区块链,个人成长">
<meta property="og:type" content="website">
<meta property="og:title" content="Tiny熊的博客">
<meta property="og:url" content="http://tinyxiong.com/index.html">
<meta property="og:site_name" content="Tiny熊的博客">
<meta property="og:description" content="关注领域：个人成长、思考、总结、区块链，Linux，移动开发，Mac">
<meta property="og:locale" content="zh-Hans">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Tiny熊的博客">
<meta name="twitter:description" content="关注领域：个人成长、思考、总结、区块链，Linux，移动开发，Mac">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.3',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":true,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://tinyxiong.com/"/>





  <title>Tiny熊的博客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left 
  page-home">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Tiny熊的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description">记录我的学习与思考</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-深入浅出区块链">
          <a href="https://learnblockchain.cn" rel="section">
            
            深入浅出区块链
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
  <section id="posts" class="posts-expand">
    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://tinyxiong.com/govern/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Tiny熊">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Tiny熊的博客">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/govern/" itemprop="url">谈谈区块链（去中心化）治理</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-06-01T14:23:00+08:00">
                2019-06-01
              </time>
            

            
              <span class="post-meta-divider">|</span>
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-check-o"></i>
              </span>
              
                <span class="post-meta-item-text">更新于&#58;</span>
              
              <time title="更新于" itemprop="dateModified" datetime="2019-06-01T22:03:31+08:00">
                2019-06-01
              </time>
            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/思考/" itemprop="url" rel="index">
                    <span itemprop="name">思考</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          <p>治理，这个词在英语系国家用的很多，国内使用比较少，原意是控制、引导和操纵，指的是在特定范围内行使权威。如何在众多不同利益共同发挥作用的领域建立一致或取得认同，以便实施某项计划。</p>
          <!--noindex-->
          <div class="post-button text-center">
            <a class="btn" href="/govern/#more" rel="contents">
              阅读全文 &raquo;
            </a>
          </div>
          <!--/noindex-->
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://tinyxiong.com/layer1-layer2 /">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Tiny熊">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Tiny熊的博客">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/layer1-layer2 /" itemprop="url">Layer 1 性能足够好，还需要Layer 2么？</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-05-13T14:23:00+08:00">
                2019-05-13
              </time>
            

            
              <span class="post-meta-divider">|</span>
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-check-o"></i>
              </span>
              
                <span class="post-meta-item-text">更新于&#58;</span>
              
              <time title="更新于" itemprop="dateModified" datetime="2019-05-16T22:49:33+08:00">
                2019-05-16
              </time>
            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>思考： Layer 1 性能足够好，还需要 Layer 2 么？</p>
<p>现在已经有一些 Layer 1 层的扩容技术已经可以大大解决性能问题，如：使用DPos共识、DAG、分片技术等，是否 Layer 2 扩容还有必要？</p>
<p>有一点是肯定的： Layer 1 不可能快过 Layer 2， 更广泛的共识，确定状态时间必定更长。<br>另外，Layer 2 还有很多不同于 Layer 1的特性， 比如：</p>
<ol>
<li>Layer 2 可以省去交易费（或很少），在这种情况下也能获得交易的即时结算（layer1 通过越快越贵）。</li>
<li>Layer 2 提供更好的隐私保护。</li>
<li>Layer 2 更好的支持批量交易。</li>
<li>利用一些 State Channel， 两方能够进行直接通信， 省去联网。</li>
</ol>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://tinyxiong.com/pow-or-pos/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Tiny熊">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Tiny熊的博客">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/pow-or-pos/" itemprop="url">PoW和PoS的大讨论 - by Jan</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-05-08T14:23:00+08:00">
                2019-05-08
              </time>
            

            
              <span class="post-meta-divider">|</span>
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-check-o"></i>
              </span>
              
                <span class="post-meta-item-text">更新于&#58;</span>
              
              <time title="更新于" itemprop="dateModified" datetime="2019-05-08T14:20:35+08:00">
                2019-05-08
              </time>
            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/学习/" itemprop="url" rel="index">
                    <span itemprop="name">学习</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>这两天在橙皮书的微信群里面产生了PoW和PoS的大讨论，甚是精彩，本文是Jan 的回复(节选)，非常精彩。</p>
<blockquote>
<p>A: POS大户坐着获得的资本利得，是小散无法逾越的鸿沟，且无法约束他的权利。</p>
<p>B: 这个问题是社会正常现象吧,有钱的人挣钱本身就是比平民容易,他投入这么多钱收入却比别人少,那才是不公平啊,现在是大家获得的比例是一样的,大户投入的钱多,就是会获得更多的回报</p>
</blockquote>
<p>这是一个PoW和PoS讨论中常见的对话。A提出了两个问题</p>
<ol>
<li>(公平性问题) PoS大户坐着获利，普通人没有；</li>
<li>(无法制约）PoS大户的权利无法约束。B回答了1。</li>
</ol>
<p>对于问题1, 我同意B的看法。投入多回报多是合理的，无论是pow还是pos都是这样， <strong>区块链是帮助实现过程公平而不是结果公平的工具</strong>。 在区块链里面我们依然会看到马太效应。试图用区块链去解决结果公平的后果是区块链治理的内涵无限扩大，将本该由协议解决的各种问题（后面会看到）统统推给投票去解决。然而无论是历史还是理论早就告诉我们，世界上并没有完美的群体选择制度。区块链是一个能够记录数据，保证数据不被篡改，为所有人提供数据的工具，通过这样一个工具帮助我们更好的实现过程公平，已经是非常非常大的进步了。</p>
<p>问题2是一个 在讨论中被忽略的关键问题。 投入资源换取回报天经地义，但前提是你想投入就能投入吗？举个例子，一个明星创业团队融资，是不是任何vc想投就能投进去呢？PoW具有非常好的开放性，使得后来的共识参与者总是可以加入共识群体，而PoS不具有这样的开放性。引用这里的讨论：</p>
<p><strong>在PoS中，未来的共识群体是由今天的共识群体决定的</strong>。任何新的节点想要参与共识都需要通过至少一个交易来实现（e.g. 抵押，投票，etc.），而这个交易是否被处理是由今天的共识群体决定，他们可以处理这个交易，也可以不处理这个交易， 如果不处理这个交易，新的节点永远都无法参与共识 。同时，“不处理交易”是容易伪装而难于惩罚的行为，我还没有看到能在共识协议中解决这个问题的方案。PoS往往是根据stake的权重来分配出块比例（和节点数量没关系），考虑到大部分系统中stake的集中程度，这是一个非常实际的问题。</p>
<p>PoW是彻底的Permissionless。无论什么时候，只要你愿意，都可以买矿机和电力加入出块者的行列，不需要今天的矿工给你任何形式的许可。你也许会说，我还是需要购买矿机和电力呀，这是不是一种形式的许可呢？是的，从更低一层来说，这也是一种许可。但遗憾的是，在所有的Proof of XXX中，这已经是最去中心化的许可形式了，毕竟矿机生产和电力资源的去中心化程度远高于各种 tokens (的分散程度)。我们总是应该追求尽可能的去中心化，否则用回中心化系统就好了。</p>
<p>”是否能够投入”本身可以看作一种option，有非常高的价值。PoW能有这样的性质，是因为工作量证明的计算是一种不依赖历史的计算， 无论你在任何时间点购买算力加入计算，你都和其他矿工在同一个起跑线上, 这是一个非常独特的、反直觉的性质，正是这个性质使得后来者总是能打破了早期参与者的优势。PoS不同，因为stake是系统中内生的资产，资产的所有权是由系统历史决定的，交易顺序是由系统历史决定的，因此无论是需要抵押token加入的pos，还是只需要持有token就可以参与的pos，其validator集合都是这个系统自身的历史决定，PoS和PoW在”参与共识是否需要依赖历史“上是截然不同的。我们很容易能看出，这是一个本质不同，无论什么样的上层设计都解决不了这一点。</p>
<p>因此PoW和PoS的设计，从根本上是两种不同的思路，体现两种不同的理念：</p>
<ul>
<li>(PoS) 系统应该让先发者获得天然的优势</li>
<li>(PoW) 系统不应该让先发者获得天然的优势</li>
</ul>
<p>注意主语是“系统”，共识参与者在系统外的优势不是任何协议设计可以解决的。</p>
<blockquote>
<p>A: pos买币staking不就是投入了吗</p>
</blockquote>
<p>确实是投入，但是这里的“投入获得回报”中的”回报”已经变了。对于投入获得的回报我们需要分开来看，一种回报是token收益，最近的pos基本都支持delegate, 所以基本上所有人都有这个option. 另一种是参与共识的权利，根据上面的回答，它可以被现有的validator垄断，大部分人没有option。权利有很多种，分红只是其中一种，而且不是关键的那一种。</p>
<p>那么共识的权利为什么重要，它有什么用呢？它意味着你可以对交易排序，而交易排序决定了链上众筹时你的交易能不能及时上链，在DEX里面交易时你的订单能不能及时成交，等等等等。在PoS的系统上做DeFi？你需要认真考虑一下validator本身是做什么的，是否和你的发送的交易有利益相关？我们知道，DeFi的交易很可能是价值含量非常高的交易，e.g. DEX里面一个巨大的买单，这时候validator如果安排一个插队的交易怎么办？仅仅是交易顺序的不同就可以造成巨大的获利，何况共识节点能做的远大于此？<strong>操控交易排序是无法被发现的</strong></p>
<p>聪明的你一定会想到：“那PoW的矿工/矿池不是一样有这样的问题吗？” 没错，PoW的矿工一样有这样的权利，但PoW有两个优点可以削弱这个问题：</p>
<ul>
<li>出块节点和生态中的关键用户是解耦的。生态中的关键用户，指的是交易所、钱包等围绕区块链提供服务的服务商，它们为大量的用户提供优质的服务，聚集了大量的用户和交易。在PoS中，由于拥有大量用户和交易，stake会自然的往生态中的关键用户集中，形成天然的stake pool，因此关键用户在业务上的优势可以转化成在共识和治理上的优势（在某些链里面已经体现的非常明显了），使得先行者的优势更加强化。在PoW中，矿工群体和交易所/钱包是独立的，他们有不同的专业分工，通过不同的专业知识，不同的方式获取回报，关键用户不能将自己的业务优势转化为协议中的优势，矿工也没办法将自己在协议中的优势转化为上层业务的优势。在PoW中，开发者、用户和共识节点之间可以形成制衡。</li>
<li>PoW的共识是开放的（见上文），充满了激烈的竞争。也许一个矿池/矿工可以在短时间内做到这一点，但由于新的矿工总是可以自由加入，想要长久的做到这一点是非常困难的，激烈竞争将会造就一个越来越公平的充分竞争的市场，这需要时间(30年？maybe)。反过来，在PoS中，由于天然的先行者的优势，以及业务优势和共识优势的绑定，先行者的优势只会越来越大，竞争将逐渐消失，最后形成垄断或寡头。在基础设施级的协议里面，我们应该尽可能的去避免垄断。</li>
</ul>
<p>区块链本身是一个大的排队机，决定排序的权利是这个系统中最关键的权利。</p>
<p>在PoS中，validator集合中的validator数目是无关紧要的，并不能代表stake分散。如果你有99%的stake，你应该把它们分散到100个甚至1000个validator上去。</p>
<p>PoW中同样会有算力集中在大矿工手中的问题，但是由于PoW的开放性以及系统没有给后来者制造劣势（见上文），这样的集中只会是暂时的，算力将在激烈的竞争中不停的从一个人手中转移到另一个人手中。PoW赞美竞争，PoW是一个开放的系统，只有开放的系统才可能远离热力学的终局 1，保持长久的生机。</p>
<p>PoS有其价值，也有其问题，因此无法适用所有场景。Layer 1的区块链协议必须使用PoW，只有使用PoW的Layer 1才能解决我们希望解决的问题，实现我们希望的未来。</p>
<p>来源：<a href="https://talk.nervos.org/t/pow-vs-pos/1732?u=janx" target="_blank" rel="noopener">https://talk.nervos.org/t/pow-vs-pos/1732?u=janx</a></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://tinyxiong.com/javascript-level/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Tiny熊">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Tiny熊的博客">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/javascript-level/" itemprop="url">【转】10个JavaScript开发者必须知晓的概念</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-05-04T23:53:00+08:00">
                2019-05-04
              </time>
            

            
              <span class="post-meta-divider">|</span>
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-check-o"></i>
              </span>
              
                <span class="post-meta-item-text">更新于&#58;</span>
              
              <time title="更新于" itemprop="dateModified" datetime="2019-05-04T23:26:51+08:00">
                2019-05-04
              </time>
            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/学习/" itemprop="url" rel="index">
                    <span itemprop="name">学习</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h2 id="立即执行函数"><a href="#立即执行函数" class="headerlink" title="立即执行函数"></a>立即执行函数</h2><p><strong>立即执行函数</strong>，即Immediately Invoked Function Expression (IIFE)，正如它的名字，就是创建函数的同时立即执行。它没有绑定任何事件，也无需等待任何异步操作：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line"> <span class="comment">// 代码</span></span><br><span class="line"> <span class="comment">// ...</span></span><br><span class="line">&#125;)();</span><br></pre></td></tr></table></figure>
<p><strong>function(){…}</strong>是一个匿名函数，包围它的一对括号将其转换为一个表达式，紧跟其后的一对括号调用了这个函数。<strong>立即执行函数</strong>也可以理解为立即调用一个匿名函数。<strong>立即执行函数</strong>最常见的应用场景就是：将var变量的作用域限制于你们函数内，这样可以避免命名冲突。</p>
<h2 id="闭包"><a href="#闭包" class="headerlink" title="闭包"></a>闭包</h2><p>对于闭包(closure)，当外部函数返回之后，内部函数依然可以访问外部函数的变量。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">f1</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line"> <span class="keyword">var</span> N = <span class="number">0</span>; <span class="comment">// N是f1函数的局部变量</span></span><br><span class="line">  </span><br><span class="line"> <span class="function"><span class="keyword">function</span> <span class="title">f2</span>(<span class="params"></span>) // <span class="title">f2</span>是<span class="title">f1</span>函数的内部函数，是闭包</span></span><br><span class="line"><span class="function">  </span>&#123;</span><br><span class="line"> N += <span class="number">1</span>; <span class="comment">// 内部函数f2中使用了外部函数f1中的变量N</span></span><br><span class="line"> <span class="built_in">console</span>.log(N);</span><br><span class="line"> &#125;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> f2;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> result = f1();</span><br><span class="line"></span><br><span class="line">result(); <span class="comment">// 输出1</span></span><br><span class="line">result(); <span class="comment">// 输出2</span></span><br><span class="line">result(); <span class="comment">// 输出3</span></span><br></pre></td></tr></table></figure>
<p>代码中，外部函数<strong>f1</strong>只执行了一次，变量<strong>N</strong>设为<strong>0</strong>，并将内部函数<strong>f2</strong>赋值给了变量<strong>result</strong>。由于外部函数<strong>f1</strong>已经执行完毕，其内部变量<strong>N</strong>应该在内存中被清除，然而事实并不是这样：我们每次调用<strong>result</strong>的时候，发现变量<strong>N</strong>一直在内存中，并且在累加。为什么呢？这就是闭包的神奇之处了！</p>
<h2 id="使用闭包定义私有变量"><a href="#使用闭包定义私有变量" class="headerlink" title="使用闭包定义私有变量"></a>使用闭包定义私有变量</h2><p>通常，JavaScript开发者使用下划线作为私有变量的前缀。但是实际上，这些变量依然可以被访问和修改，并非真正的私有变量。这时，使用闭包可以定义真正的私有变量：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Product</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">var</span> name;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">this</span>.setName = <span class="function"><span class="keyword">function</span>(<span class="params">value</span>) </span>&#123;</span><br><span class="line"> name = value;</span><br><span class="line"> &#125;;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">this</span>.getName = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line"> <span class="keyword">return</span> name;</span><br><span class="line"> &#125;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> p = <span class="keyword">new</span> Product();</span><br><span class="line">p.setName(<span class="string">"Fundebug"</span>);</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(p.name); <span class="comment">// 输出undefined</span></span><br><span class="line"><span class="built_in">console</span>.log(p.getName()); <span class="comment">// 输出Fundebug</span></span><br></pre></td></tr></table></figure>
<p>代码中，对象<strong>p</strong>的的<strong>name</strong>属性为私有属性，使用<strong>p.name</strong>不能直接访问。</p>
<h2 id="prototype"><a href="#prototype" class="headerlink" title="prototype"></a>prototype</h2><p>每个JavaScript构造函数都有一个<strong>prototype</strong>属性，用于设置所有实例对象需要共享的属性和方法。<strong>prototype</strong>属性不能列举。JavaScript仅支持通过<strong>prototype</strong>属性进行继承属性和方法。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Rectangle</span>(<span class="params">x, y</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line"> <span class="keyword">this</span>._length = x;</span><br><span class="line"> <span class="keyword">this</span>._breadth = y;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Rectangle.prototype.getDimensions = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line"> <span class="keyword">return</span> &#123;</span><br><span class="line"> length: <span class="keyword">this</span>._length,</span><br><span class="line"> breadth: <span class="keyword">this</span>._breadth</span><br><span class="line"> &#125;;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> x = <span class="keyword">new</span> Rectangle(<span class="number">3</span>, <span class="number">4</span>);</span><br><span class="line"><span class="keyword">var</span> y = <span class="keyword">new</span> Rectangle(<span class="number">4</span>, <span class="number">3</span>);</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(x.getDimensions()); <span class="comment">// &#123; length: 3, breadth: 4 &#125;</span></span><br><span class="line"><span class="built_in">console</span>.log(y.getDimensions()); <span class="comment">// &#123; length: 4, breadth: 3 &#125;</span></span><br></pre></td></tr></table></figure>
<p>代码中，<strong>x</strong>和<strong>y</strong>都是构造函数<strong>Rectangle</strong>创建的对象实例，它们通过prototype继承了getDimensions方法。</p>
<h2 id="模块化"><a href="#模块化" class="headerlink" title="模块化"></a>模块化</h2><p>JavaScript并非模块化编程语言，至少ES6落地之前都不是。然而对于一个复杂的Web应用，模块化编程是一个最基本的要求。这时，可以使用<strong>立即执行函数</strong>来实现模块化，正如很多JS库比如<a href="https://github.com/jquery/jquery" target="_blank" rel="noopener">jQuery</a>以及我们<a href="https://www.fundebug.com/" target="_blank" rel="noopener">Fundebug</a>都是这样实现的。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> <span class="built_in">module</span> = (<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line"> <span class="keyword">var</span> N = <span class="number">5</span>;</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">function</span> <span class="title">print</span>(<span class="params">x</span>) </span>&#123;</span><br><span class="line"> <span class="built_in">console</span>.log(<span class="string">"The result is: "</span> + x);</span><br><span class="line"> &#125;</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">function</span> <span class="title">add</span>(<span class="params">a</span>) </span>&#123;</span><br><span class="line"> <span class="keyword">var</span> x = a + N;</span><br><span class="line"> print(x);</span><br><span class="line"> &#125;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> &#123;</span><br><span class="line"> description: <span class="string">"This is description"</span>,</span><br><span class="line"> add: add</span><br><span class="line"> &#125;;</span><br><span class="line">&#125;)();</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">module</span>.description); <span class="comment">// 输出"this is description" </span></span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.add(<span class="number">5</span>); <span class="comment">// 输出“The result is: 10”</span></span><br></pre></td></tr></table></figure>
<p>所谓模块化，就是根据需要控制模块内属性与方法的可访问性，即私有或者公开。在代码中，module为一个独立的模块，<strong>N</strong>为其私有属性，<strong>print</strong>为其私有方法，<strong>decription</strong>为其公有属性，<strong>add</strong>为其共有方法。</p>
<h2 id="变量提升"><a href="#变量提升" class="headerlink" title="变量提升"></a>变量提升</h2><p>JavaScript会将所有变量和函数声明移动到它的作用域的最前面，这就是所谓的<strong>变量提升(Hoisting)</strong>。也就是说，无论你在什么地方声明变量和函数，解释器都会将它们移动到作用域的最前面。因此我们可以先使用变量和函数，而后声明它们。</p>
<p>但是，仅仅是变量声明被提升了，而变量赋值不会被提升。如果你不明白这一点，有时则会出错：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(y);  <span class="comment">// 输出undefined</span></span><br><span class="line"></span><br><span class="line">y = <span class="number">2</span>; <span class="comment">// 初始化y</span></span><br></pre></td></tr></table></figure>
<p>上面的代码等价于下面的代码：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> y;  <span class="comment">// 声明y</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(y);  <span class="comment">// 输出undefined</span></span><br><span class="line"></span><br><span class="line">y = <span class="number">2</span>; <span class="comment">// 初始化y</span></span><br></pre></td></tr></table></figure>
<p>为了避免BUG，开发者应该在每个作用域开始时声明变量和函数。</p>
<h2 id="柯里化"><a href="#柯里化" class="headerlink" title="柯里化"></a>柯里化</h2><p>柯里化，即<strong>Currying</strong>，可以是函数变得更加灵活。我们可以一次性传入多个参数调用它；也可以只传入一部分参数来调用它，让它返回一个函数去处理剩下的参数。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> add = <span class="function"><span class="keyword">function</span>(<span class="params">x</span>) </span>&#123;</span><br><span class="line"> <span class="keyword">return</span> <span class="function"><span class="keyword">function</span>(<span class="params">y</span>) </span>&#123;</span><br><span class="line"> <span class="keyword">return</span> x + y;</span><br><span class="line"> &#125;;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(add(<span class="number">1</span>)(<span class="number">1</span>)); <span class="comment">// 输出2</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> add1 = add(<span class="number">1</span>);</span><br><span class="line"><span class="built_in">console</span>.log(add1(<span class="number">1</span>)); <span class="comment">// 输出2</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> add10 = add(<span class="number">10</span>);</span><br><span class="line"><span class="built_in">console</span>.log(add10(<span class="number">1</span>)); <span class="comment">// 输出11</span></span><br></pre></td></tr></table></figure>
<p>代码中，我们可以一次性传入2个1作为参数<strong>add(1)(1)</strong>，也可以传入1个参数之后获取<strong>add1</strong>与<strong>add10</strong>函数，这样使用起来非常灵活。</p>
<h2 id="apply-call与bind方法"><a href="#apply-call与bind方法" class="headerlink" title="apply, call与bind方法"></a>apply, call与bind方法</h2><p>JavaScript开发者有必要理解<strong>apply</strong>、<strong>call</strong>与<strong>bind</strong>方法的不同点。它们的共同点是第一个参数都是<strong>this</strong>，即函数运行时依赖的上下文。</p>
<p>三者之中，<strong>call</strong>方法是最简单的，它等价于指定<strong>this</strong>值调用函数：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> user = &#123;</span><br><span class="line"> name: <span class="string">"Rahul Mhatre"</span>,</span><br><span class="line"> whatIsYourName: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line"> <span class="built_in">console</span>.log(<span class="keyword">this</span>.name);</span><br><span class="line"> &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">user.whatIsYourName(); <span class="comment">// 输出"Rahul Mhatre",</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> user2 = &#123;</span><br><span class="line"> name: <span class="string">"Neha Sampat"</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">user.whatIsYourName.call(user2); <span class="comment">// 输出"Neha Sampat"</span></span><br></pre></td></tr></table></figure>
<p><strong>apply</strong>方法与<strong>call</strong>方法类似。两者唯一的不同点在于，<strong>apply</strong>方法使用数组指定参数，而<strong>call</strong>方法每个参数单独需要指定：</p>
<ul>
<li><strong>apply(thisArg, [argsArray])</strong></li>
<li><strong>call(thisArg, arg1, arg2, …)</strong></li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> user = &#123;</span><br><span class="line"> greet: <span class="string">"Hello!"</span>,</span><br><span class="line"> greetUser: <span class="function"><span class="keyword">function</span>(<span class="params">userName</span>) </span>&#123;</span><br><span class="line"> <span class="built_in">console</span>.log(<span class="keyword">this</span>.greet + <span class="string">" "</span> + userName);</span><br><span class="line"> &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> greet1 = &#123;</span><br><span class="line"> greet: <span class="string">"Hola"</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">user.greetUser.call(greet1, <span class="string">"Rahul"</span>); <span class="comment">// 输出"Hola Rahul"</span></span><br><span class="line">user.greetUser.apply(greet1, [<span class="string">"Rahul"</span>]); <span class="comment">// 输出"Hola Rahul"</span></span><br></pre></td></tr></table></figure>
<p>使用<strong>bind</strong>方法，可以为函数绑定<strong>this</strong>值，然后作为一个新的函数返回：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> user = &#123;</span><br><span class="line"> greet: <span class="string">"Hello!"</span>,</span><br><span class="line"> greetUser: <span class="function"><span class="keyword">function</span>(<span class="params">userName</span>) </span>&#123;</span><br><span class="line"> <span class="built_in">console</span>.log(<span class="keyword">this</span>.greet + <span class="string">" "</span> + userName);</span><br><span class="line"> &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> greetHola = user.greetUser.bind(&#123;<span class="attr">greet</span>: <span class="string">"Hola"</span>&#125;);</span><br><span class="line"><span class="keyword">var</span> greetBonjour = user.greetUser.bind(&#123;<span class="attr">greet</span>: <span class="string">"Bonjour"</span>&#125;);</span><br><span class="line"></span><br><span class="line">greetHola(<span class="string">"Rahul"</span>) <span class="comment">// 输出"Hola Rahul"</span></span><br><span class="line">greetBonjour(<span class="string">"Rahul"</span>) <span class="comment">// 输出"Bonjour Rahul"</span></span><br></pre></td></tr></table></figure>
<h2 id="Memoization"><a href="#Memoization" class="headerlink" title="Memoization"></a>Memoization</h2><p><strong>Memoization</strong>用于优化比较耗时的计算，通过将计算结果缓存到内存中，这样对于同样的输入值，下次只需要中内存中读取结果。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">memoizeFunction</span>(<span class="params">func</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line"> <span class="keyword">var</span> cache = &#123;&#125;;</span><br><span class="line"> <span class="keyword">return</span> <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">  </span>&#123;</span><br><span class="line"> <span class="keyword">var</span> key = <span class="built_in">arguments</span>[<span class="number">0</span>];</span><br><span class="line"> <span class="keyword">if</span> (cache[key])</span><br><span class="line"> &#123;</span><br><span class="line"> <span class="keyword">return</span> cache[key];</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> &#123;</span><br><span class="line"> <span class="keyword">var</span> val = func.apply(<span class="keyword">this</span>, <span class="built_in">arguments</span>);</span><br><span class="line"> cache[key] = val;</span><br><span class="line"> <span class="keyword">return</span> val;</span><br><span class="line"> &#125;</span><br><span class="line"> &#125;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> fibonacci = memoizeFunction(<span class="function"><span class="keyword">function</span>(<span class="params">n</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line"> <span class="keyword">return</span> (n === <span class="number">0</span> || n === <span class="number">1</span>) ? n : fibonacci(n - <span class="number">1</span>) + fibonacci(n - <span class="number">2</span>);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(fibonacci(<span class="number">100</span>)); <span class="comment">// 输出354224848179262000000</span></span><br><span class="line"><span class="built_in">console</span>.log(fibonacci(<span class="number">100</span>)); <span class="comment">// 输出354224848179262000000</span></span><br></pre></td></tr></table></figure>
<p>代码中，第2次计算<strong>fibonacci(100)</strong>则只需要在内存中直接读取结果。</p>
<h2 id="函数重载"><a href="#函数重载" class="headerlink" title="函数重载"></a>函数重载</h2><p>所谓<strong>函数重载(method overloading)</strong>，就是函数名称一样，但是输入输出不一样。或者说，允许某个函数有各种不同输入，根据不同的输入，返回不同的结果。凭直觉，<strong>函数重载</strong>可以通过<strong>if…else</strong>或者<strong>switch</strong>实现，这就不去管它了。jQuery之父John Resig提出了一个非常巧(bian)妙(tai)的方法，利用了闭包。</p>
<p>从效果上来说，<strong>people</strong>对象的<strong>find</strong>方法允许3种不同的输入: 0个参数时，返回所有人名；1个参数时，根据firstName查找人名并返回；2个参数时，根据完整的名称查找人名并返回。</p>
<p>难点在于，<strong>people.find</strong>只能绑定一个函数，那它为何可以处理3种不同的输入呢？它不可能同时绑定3个函数<strong>find0</strong>,<strong>find1</strong>与<strong>find2</strong>啊！这里的关键在于<strong>old</strong>属性。</p>
<p>由<strong>addMethod</strong>函数的调用顺序可知，<strong>people.find</strong>最终绑定的是<strong>find2</strong>函数。然而，在绑定<strong>find2</strong>时，<strong>old</strong>为<strong>find1</strong>；同理，绑定<strong>find1</strong>时，<strong>old</strong>为<strong>find0</strong>。3个函数<strong>find0</strong>,<strong>find1</strong>与<strong>find2</strong>就这样通过闭包链接起来了。</p>
<p>根据<strong>addMethod</strong>的逻辑，当<strong>f.length</strong>与<strong>arguments.length</strong>不匹配时，就会去调用<strong>old</strong>，直到匹配为止。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">addMethod</span>(<span class="params">object, name, f</span>)</span></span><br><span class="line"><span class="function"></span>&#123;　　</span><br><span class="line"> <span class="keyword">var</span> old = object[name];</span><br><span class="line"> object[name] = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">  </span>&#123;</span><br><span class="line"> <span class="comment">// f.length为函数定义时的参数个数</span></span><br><span class="line"> <span class="comment">// arguments.length为函数调用时的参数个数　　　　</span></span><br><span class="line"> <span class="keyword">if</span> (f.length === <span class="built_in">arguments</span>.length)</span><br><span class="line"> &#123;</span><br><span class="line"> <span class="keyword">return</span> f.apply(<span class="keyword">this</span>, <span class="built_in">arguments</span>);</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span> (<span class="keyword">typeof</span> old === <span class="string">"function"</span>)</span><br><span class="line"> &#123;</span><br><span class="line"> <span class="keyword">return</span> old.apply(<span class="keyword">this</span>, <span class="built_in">arguments</span>);</span><br><span class="line"> &#125;</span><br><span class="line"> &#125;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 不传参数时，返回所有name</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">find0</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;　　</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">this</span>.names;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 传一个参数时，返回firstName匹配的name</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">find1</span>(<span class="params">firstName</span>)</span></span><br><span class="line"><span class="function"></span>&#123;　　</span><br><span class="line"> <span class="keyword">var</span> result = [];</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i <span class="keyword">this</span>.names.length; i++)</span><br><span class="line"> &#123;</span><br><span class="line"> <span class="keyword">if</span> (<span class="keyword">this</span>.names[i].indexOf(firstName) === <span class="number">0</span>)</span><br><span class="line"> &#123;</span><br><span class="line"> result.push(<span class="keyword">this</span>.names[i]);</span><br><span class="line"> &#125;</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="keyword">return</span> result;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 传两个参数时，返回firstName和lastName都匹配的name</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">find2</span>(<span class="params">firstName, lastName</span>)</span></span><br><span class="line"><span class="function"></span>&#123;　</span><br><span class="line"> <span class="keyword">var</span> result = [];</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i <span class="keyword">this</span>.names.length; i++)</span><br><span class="line"> &#123;</span><br><span class="line"> <span class="keyword">if</span> (<span class="keyword">this</span>.names[i] === (firstName + <span class="string">" "</span> + lastName))</span><br><span class="line"> &#123;</span><br><span class="line"> result.push(<span class="keyword">this</span>.names[i]);</span><br><span class="line"> &#125;</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="keyword">return</span> result;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> people = &#123;　　</span><br><span class="line"> names: [<span class="string">"Dean Edwards"</span>, <span class="string">"Alex Russell"</span>, <span class="string">"Dean Tom"</span>]</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">addMethod(people, <span class="string">"find"</span>, find0);</span><br><span class="line">addMethod(people, <span class="string">"find"</span>, find1);</span><br><span class="line">addMethod(people, <span class="string">"find"</span>, find2);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(people.find()); <span class="comment">// 输出["Dean Edwards", "Alex Russell", "Dean Tom"]</span></span><br><span class="line"><span class="built_in">console</span>.log(people.find(<span class="string">"Dean"</span>)); <span class="comment">// 输出["Dean Edwards", "Dean Tom"]</span></span><br><span class="line"><span class="built_in">console</span>.log(people.find(<span class="string">"Dean"</span>, <span class="string">"Edwards"</span>)); <span class="comment">// 输出["Dean Edwards"]</span></span><br></pre></td></tr></table></figure>
<h2 id="参考链接"><a href="#参考链接" class="headerlink" title="参考链接"></a>参考链接</h2><ul>
<li><a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Closures" target="_blank" rel="noopener">闭包 - MDN</a></li>
<li><a href="http://www.htmleaf.com/ziliaoku/qianduanjiaocheng/201512282961.html" target="_blank" rel="noopener">JavaScript闭包-块级作用域和私有变量</a></li>
<li><a href="http://www.ruanyifeng.com/blog/2011/06/designing_ideas_of_inheritance_mechanism_in_javascript.html" target="_blank" rel="noopener">Javascript继承机制的设计思想 - 阮一峰</a></li>
<li><a href="https://developer.mozilla.org/zh-CN/docs/Glossary/Hoisting" target="_blank" rel="noopener">变量提升 - MDN</a></li>
<li><a href="https://www.gitbook.com/book/llh911001/mostly-adequate-guide-chinese/details" target="_blank" rel="noopener">JS函数式编程指南</a></li>
<li><a href="http://www.cnblogs.com/yugege/p/5539020.html" target="_blank" rel="noopener">浅谈JavaScript函数重载</a></li>
</ul>
<p>本文链接地址：<a href="https://blog.fundebug.com/2017/07/17/10-javascript-difficulties/" target="_blank" rel="noopener">https://blog.fundebug.com/2017/07/17/10-javascript-difficulties/</a></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://tinyxiong.com/eip712/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Tiny熊">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Tiny熊的博客">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/eip712/" itemprop="url">理解 EIP712</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-04-25T23:53:00+08:00">
                2019-04-25
              </time>
            

            
              <span class="post-meta-divider">|</span>
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-check-o"></i>
              </span>
              
                <span class="post-meta-item-text">更新于&#58;</span>
              
              <time title="更新于" itemprop="dateModified" datetime="2019-04-25T11:23:18+08:00">
                2019-04-25
              </time>
            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/学习/" itemprop="url" rel="index">
                    <span itemprop="name">学习</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>这个EIP 是为了提高链下消息签名对链上的可用性。因为节省gas以及减少链上交易的原因，采用链下消息签名的需求越来越多。</p>
<p>我们知道对数据签名后，它会变成一段无法阅读的字节字符串，而有时候，我们需要知道背后所代码数据的含义，下面这个图很好表达了这个对比：</p>
<p><img src="https://img.learnblockchain.cn/2019/15561615006261.jpg" alt=""></p>
<p>图片来源于<a href="https://ethereum-magicians.org/t/eip-712-eth-signtypeddata-as-a-standard-for-machine-verifiable-and-human-readable-typed-data-signing/397" target="_blank" rel="noopener">eip-712 讨论</a></p>
<p>为了签名结构化数据，EIP712 中加入了新的远程调用用法： eth_signTypedData， web3.js 也有对应的实现：web3.eth.signTypedData。</p>
<p>如何结构化数据可以参考<a href="https://eips.ethereum.org/EIPS/eip-712" target="_blank" rel="noopener">eip-712 标准</a>，这里也有一篇文章介绍：<a href="https://learnblockchain.cn/2019/04/24/token-EIP712/" target="_blank" rel="noopener">理解 EIP712 - 类型结构化数据 Hash 与签名</a></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://tinyxiong.com/seo/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Tiny熊">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Tiny熊的博客">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/seo/" itemprop="url">SEO 经验总结</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-04-12T23:53:00+08:00">
                2019-04-12
              </time>
            

            
              <span class="post-meta-divider">|</span>
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-check-o"></i>
              </span>
              
                <span class="post-meta-item-text">更新于&#58;</span>
              
              <time title="更新于" itemprop="dateModified" datetime="2019-04-14T17:17:11+08:00">
                2019-04-14
              </time>
            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/学习/" itemprop="url" rel="index">
                    <span itemprop="name">学习</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          <p>搜索引擎能链接到内容不代表会收录<br>收录不代表有好的排名，必须要努力提升网站权重。</p>
<p>因为做<a href="https://learnblockchain.cn/" target="_blank" rel="noopener">深入浅出区块链 - 技术博客</a>的缘故，想有更好的搜索引擎收录，读了《SEO实战密码》，做一个笔记整理。</p>
          <!--noindex-->
          <div class="post-button text-center">
            <a class="btn" href="/seo/#more" rel="contents">
              阅读全文 &raquo;
            </a>
          </div>
          <!--/noindex-->
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://tinyxiong.com/de-social/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Tiny熊">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Tiny熊的博客">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/de-social/" itemprop="url">畅想去中心化社会</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-04-02T23:53:00+08:00">
                2019-04-02
              </time>
            

            
              <span class="post-meta-divider">|</span>
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-check-o"></i>
              </span>
              
                <span class="post-meta-item-text">更新于&#58;</span>
              
              <time title="更新于" itemprop="dateModified" datetime="2019-04-12T21:45:34+08:00">
                2019-04-12
              </time>
            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/思考/" itemprop="url" rel="index">
                    <span itemprop="name">思考</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h1 id="畅想去中心化社会"><a href="#畅想去中心化社会" class="headerlink" title="畅想去中心化社会"></a>畅想去中心化社会</h1><p>畅想一下未来的去中心化社会，政府广泛使用去中心化技术提供服务，<br>政府成为一个自动运行的社会系统，每人都将拥有一个身份id（在去中心化网络上），我们的身份特征（如指纹，面部识别）就是私钥。</p>
<p>出门就再也不用带身份证，<br>毕业证自动签发关联到身份id；<br>办护照只需要向相应的智能合约提交申请；<br>办结婚证只需要两个人在婚姻智能合约上签个名；</p>
<p>不再需要监督、审查人员，杜绝腐败，大大提高效率。</p>
<p>并且这些信息还可以跨链授权，比如以太坊可跨链访问到政府链上的应用信息，我们在借Dai 就有更高的限额。</p>
<p>（政府只是去中心化网络的维护者，就像公链的项目方，规则的制定者，财产和数据依旧属于我们自己）</p>
<p>其实一切正在悄然发生~</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://tinyxiong.com/web3/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Tiny熊">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Tiny熊的博客">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/web3/" itemprop="url">未来网络Web3技术栈</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-03-28T23:53:00+08:00">
                2019-03-28
              </time>
            

            
              <span class="post-meta-divider">|</span>
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-check-o"></i>
              </span>
              
                <span class="post-meta-item-text">更新于&#58;</span>
              
              <time title="更新于" itemprop="dateModified" datetime="2019-03-29T23:04:18+08:00">
                2019-03-29
              </time>
            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/学习/" itemprop="url" rel="index">
                    <span itemprop="name">学习</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>web3 是在互联网世界之上再造一层世界</p>
<p><img src="/images/15538708716003.jpg" alt=""></p>
<p>web3的第0层， 是p2p网络消息，和基本虚拟机层<br>web3的第1层，去中心后数据通信，存储，货币协议<br>web3的第2层是链下拓展协议</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://tinyxiong.com/cross_chain/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Tiny熊">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Tiny熊的博客">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/cross_chain/" itemprop="url">再聊聊跨链</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-03-28T23:53:00+08:00">
                2019-03-28
              </time>
            

            
              <span class="post-meta-divider">|</span>
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-check-o"></i>
              </span>
              
                <span class="post-meta-item-text">更新于&#58;</span>
              
              <time title="更新于" itemprop="dateModified" datetime="2019-03-29T22:20:33+08:00">
                2019-03-29
              </time>
            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/思考/" itemprop="url" rel="index">
                    <span itemprop="name">思考</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>再聊聊跨链：Polkadot和Cosmos 两个跨链的头部项目，他们都认为自己是下一代的互联网，他们构建的像今天TCP/IP协议一样的协议。</p>
<p>虽然我很关注这两个项目， 不过也引发我的思考， 我们是否会那么需要跨链。<br>如果把链比作一座山，跨链就是在山和山之间修桥， 跨链的前提是已经有很多的山了， 在这个山的人想去另一座山。 </p>
<p>与跨链不同，侧链可以认为是修建索道。跨链 侧链看起来是类似的事情，他们的思路却完全不同， 在跨链中，跨链本身的链是主干，所接入的链都是分支， 而侧链自己是旁支，他需要接入一个主干。</p>
<p>在跨链看来未来可能有一两座大大的山，山里已经有完整的生态，可以自由的玩耍，串门只要有索道就可以。</p>
<p>短期来看，侧链较容易落地，不过区块链本身还在早期，未来是怎样，我们先交给时间。</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://tinyxiong.com/oracle/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Tiny熊">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Tiny熊的博客">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/oracle/" itemprop="url">预言机的思考</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-03-28T23:53:00+08:00">
                2019-03-28
              </time>
            

            
              <span class="post-meta-divider">|</span>
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-check-o"></i>
              </span>
              
                <span class="post-meta-item-text">更新于&#58;</span>
              
              <time title="更新于" itemprop="dateModified" datetime="2019-03-29T22:21:31+08:00">
                2019-03-29
              </time>
            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/思考/" itemprop="url" rel="index">
                    <span itemprop="name">思考</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>我们知道所有的数据在经过节点的验证之后，才能上链。<br>而节点验证达成共识的一个条件是：验证时数据输入是相同的。这让区块链上只能使用链内的确定性的数据，因为链外的数据没法保证在不同的时间进行验证时数据的一致性。</p>
<p>这让区块链和我们的现实世界产生了割裂。</p>
<p>预言机就是这样一座桥（怎么总是喜欢用桥来比喻？），联通着区块链和现实世界。 </p>
<p>如果区块链（智能合约）想要基于某个外部状态做些事情（比如Dai项目依靠外部的币价信息），它不能主动去拿外部数据，而是要依靠<strong>预言机</strong>把外部数据以交易的形式发送进来，从而把一个不确定的数据转化为一个确定的输入。</p>
<p>好了，问题来了，我们说区块链创造了信任，就是因为去中介，现在又引入一个预言机中介，好像是一个讽刺，如何来保证预言机没有作恶呢（加入外部数据是真实的，指如何确保预言机不会篡改外部数据）？</p>
<p>这也是为什么说区块链还在早期，成熟的预言机机制会极大的促进区块链的发展，不少团队正在探索去中心化预言机，未来值得期待。</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
  </section>

  
  <nav class="pagination">
    <span class="page-number current">1</span><a class="page-number" href="/page/2/">2</a><a class="extend next" rel="next" href="/page/2/"><i class="fa fa-angle-right"></i></a>
  </nav>



          </div>
          


          

        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">Tiny熊</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">20</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">6</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">21</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          <div class="links-of-author motion-element">
            
              
                <span class="links-of-author-item">
                  <a href="https://github.com/xilibi2003" target="_blank" title="GitHub">
                    
                      <i class="fa fa-fw fa-github"></i>GitHub</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="http://weibo.com/xilibi2003" target="_blank" title="Weibo">
                    
                      <i class="fa fa-fw fa-weibo"></i>Weibo</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="https://www.zhihu.com/people/xiong-li-bing/activities" target="_blank" title="ZhiHu">
                    
                      <i class="fa fa-fw fa-globe"></i>ZhiHu</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="mailto:xilibi2003@gmail.com" target="_blank" title="E-Mail">
                    
                      <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                </span>
              
            
          </div>

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                友情链接
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="https://xiaozhuanlan.com/linuxskill" title="我的小专栏" target="_blank">我的小专栏</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://learnblockchain.cn" title="深入浅出区块链" target="_blank">深入浅出区块链</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://blog.forecho.com" title="forecho的独立博客" target="_blank">forecho的独立博客</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
        </div>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Tiny熊</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a></div>




        







  <div style="display: none;">
    <script src="//s95.cnzz.com/z_stat.php?id=1271309313&web_id=1271309313" language="JavaScript"></script>
  </div>



        
      </div>
    </footer>

    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.3"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.3"></script>



  

  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.3"></script>



  


  




	





  





  












  





  

  

  
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>


  
  

  

  

  

</body>
</html>
